<template>
  <div id="my">
    <el-form ref="form" :model="form" label-width="80px">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:8088/user/avatar"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <el-form-item label="用户名">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="form.address"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: 'My',
    data() {
      return {
        // imageUrl: 'https://img1.baidu.com/it/u=1919509102,1927615551&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500',
        imageUrl: '',
        form: {
          id: '1663114408961826817'
        }
      }
    },
    methods: {
      handleAvatarSuccess(res, file) {
        // this.imageUrl = URL.createObjectURL(file.raw);
        console.log(res);
        console.log(file);
        this.imageUrl = res.data;
        this.form.avatar = res.data;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isPNG = file.type === 'image/png';
        const isGIF = file.type === 'image/gif';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG && !isPNG && !isGIF) {
          this.$message.error('上传头像图片只能是 图片 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        console.log(isJPG || isPNG || isGIF && isLt2M)
        return isJPG || isPNG || isGIF && isLt2M;
      },
      onSubmit() {
        console.log('submit!');
        this.$store.dispatch('user/updateInfo', this.form);
      }
    },
  }
</script>

<style>
  /* el-upload使用scoped没有效果 */
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    /* 设置头像水平居中显示 */
    width: 160px;
    display: block;
    margin: 30px auto;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 160px;
    height: 160px;
    line-height: 160px;
    text-align: center;
  }
  .avatar {
    width: 160px;
    height: 160px;
    display: block;
  }
</style>

<style lang="scss" scoped>
  #my {
    height: 100%;
    position: relative;
    // 隐藏表单的滚动条
    ::-webkit-scrollbar {
        width: 0px;
    }
    .el-form {
      width: 660px;
      height: 500px;
      border: 1px solid #ccc;
      border-radius: 8px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      overflow: scroll;
      .el-form-item {
        width: 500px;
        // margin: 0 auto;
        margin-left: 50px;
      }
    }
  }
</style>